<?php
 
namespace Modules\AuthAdmin\Models;
use Modules\AuthAdmin\Services\CommonService;
use Illuminate\Support\Facades\Cache;
use Modules\ShopAdmin\Models\ShopUser as ShopUserModel;
use Modules\ShopApi\Models\ShopVisit as ShopVisitModel;
class AuthConfig extends BaseApiModel
{
	/** 
     * @name  关联图片
     */
    public function image_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','image_id');
    }
	/** 
     * @name  关联图片
     */
    public function gzh_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','gzh_id');
    }
	/** 
     * @name  关联图片
     */
    public function xcx_one()
    {
        return $this->hasOne('Modules\AuthAdmin\Models\AuthImage','id','xcx_id');
    }
	/**
	 * @name  系统配置
	 * @return  JSON
	 */
	public function edit_data(){
		$info = $this->select('id','name','image_status','image_id','gzh_id','xcx_id','about_us')->with([
			'image_one'=>function($query){
				$query->select('id','url','open');  
			},
			'gzh_one'=>function($query){
				$query->select('id','url','open');  
			},
			'xcx_one'=>function($query){
				$query->select('id','url','open');  
			}
		])->find(1);
		$CommonService = new CommonService();
		$arr = [
			'id'=>$info['id'],
			'name'=>$info['name'],
			'image_status'=>$info['image_status'],
			'image_id'=>$info['image_id'],
			'gzh_id'=>$info['gzh_id'],
			'xcx_id'=>$info['xcx_id'],
			'about_us'=>$CommonService->replace_pic_url($info['about_us']),
		];
		$http = $CommonService->get_http_type();
		if($info['image_one']['open'] == 1){
			$arr['image_url'] = $http.$info['image_one']['url'];
		}else{
			$arr['image_url'] = $info['image_one']['url'];
		}
		if($info['gzh_one']['open'] == 1){
			$arr['gzh_url'] = $http.$info['gzh_one']['url'];
		}else{
			$arr['gzh_url'] = $info['gzh_one']['url'];
		}
		if($info['xcx_one']['open'] == 1){
			$arr['xcx_url'] = $http.$info['xcx_one']['url'];
		}else{
			$arr['xcx_url'] = $info['xcx_one']['url'];
		}
		return $this->api_success(['data'=>$arr]);
	}
	/**
	 * @name  获取配置信息
	 * @return  JSON
	 */
	public function config_info(){
		return $this->api_success(['data'=>$this->config_info_data()]);
	}
	/**
	 * @name  配置信息
	 * @return  JSON
	 */
	public function config_info_data(){
		$config_info = Cache::get('config_info');
		if(!$config_info){
			$info = $this->select('id','name','image_status','image_id')->with([
				'image_one'=>function($query){
					$query->select('id','url','open');  
				}
			])->find(1);
			$config_info = [
				'name'=>$info['name'],
				'image_status'=>$info['image_status']
			];
			if($info['image_one']['open'] == 1){
				$config_info['image_url'] = (new CommonService())->get_http_type().$info['image_one']['url'];
			}else{
				$config_info['image_url'] = $info['image_one']['url'];
			}
			Cache::put('config_info',$config_info,3600);
		}
		return $config_info;
	}
	/**
	 * @name  清除缓存
	 * @return  JSON
	 */
	public function out_cache(){
		Cache::forget('config_info');
		Cache::forget('area_list');
		return $this->api_success(['msg'=>'清除成功！']);
	}
	public function main(){
		$info = $this->select('id','gzh_id','xcx_id')->with([
			'gzh_one'=>function($query){
				$query->select('id','url','open');  
			},
			'xcx_one'=>function($query){
				$query->select('id','url','open');  
			}
		])->find(1);
		$arr = [];
		$CommonService = new CommonService();
		$http = $CommonService->get_http_type();
		if($info['gzh_one']['open'] == 1){
			$arr['gzh_url'] = $http.$info['gzh_one']['url'];
		}else{
			$arr['gzh_url'] = $info['gzh_one']['url'];
		}
		if($info['xcx_one']['open'] == 1){
			$arr['xcx_url'] = $http.$info['xcx_one']['url'];
		}else{
			$arr['xcx_url'] = $info['xcx_one']['url'];
		}
		$arr['user_sum'] = ShopUserModel::count();
		$arr['month_user_sum'] = ShopUserModel::whereBetween('created_at',[date('Y-m') . '-01 00:00:00',date('Y-m-d H:i:s')])->count(); 
		$arr['visit_sum'] = ShopVisitModel::count();
		$arr['month_visit_sum'] = ShopVisitModel::whereBetween('created_at',[date('Y-m') . '-01 00:00:00',date('Y-m-d H:i:s')])->count();
		return $this->api_success(['data'=>$arr]);  
	}
}
